import {createRouter,createWebHashHistory,RouteRecordRaw} from 'vue-router'
import {ElMessage} from "element-plus";
import {doLogin, getUserInfo} from "@/api/auth/login.ts";
import Layout from "@/layout/index.vue";
import {markRaw, reactive} from "vue";

// 声明导入的类型
declare module 'vue' {
    interface ImportMeta {
        glob: (pattern: string) => Promise<Record<string, () => Promise<any>>>;
    }
}


const routes: Array<RouteRecordRaw> = [
    {
        path:"/",
        name:"chunk",
        component: Layout,
        children:[
            {
                path:"/fileList",
                name:"FileList",
                component:()=>import('@/views/fileList/file.vue')
            },{
                path:"/dashboard",
                name:"Dashboard",
                component:()=>import('@/views/dashboard/index.vue')
            },  {
                path:"/upload",
                name:"Upload",
                component:()=>import('@/views/upload/upload.vue')
            }, {
                path:"/person",
                name:"Person",
                component:()=>import('@/views/person/index.vue')
            }, {
                path:"/recycle",
                name:"Recycle",
                component:()=>import('@/views/recycle/recycle.vue')
            }
        ]
    },
    {
        path:"/login",
        name:"Login",
        component: ()=>import('@/views/login/login.vue'),
    }
];

const dynamicRouter: Array<RouteRecordRaw> = [
    {
        path:"/addUser",
        name:"AddUser",
        component:()=>import('@/views/addUser/addUser.vue')
    }
]

const router = createRouter({
    history:createWebHashHistory("/dist"),
    routes,
})

interface Sidebar{
    icon:any,
    title:String,
    path:String,
}

// 在用户登出时销毁所有动态路由
export function destroyDynamicRoutes() {
    router.getRoutes().forEach(route => {
        dynamicRouter.forEach((item:any)=>{
            if (route.path === item.path){
                router.removeRoute(route.name||'')
            }
        })
    });
    sidebar = []
}

export let sidebar:Sidebar[] = reactive([])
router.beforeEach((to,{},next)=>{
    if (to.path === '/login'){
        next()
    }else {
        doLogin().then(res=>{
            if (res.data.code === 200){
                getUserInfo().then(res=>{
                    if (res.data.code === 200){
                        const menus = res.data.data.menus
                        let modules:any = dynamicRouter
                        if(menus.length === 0){
                            next()
                        }else {
                            menus.forEach((item:any) =>{
                                modules.forEach((res:any)=>{
                                    if (res.name === item.name){
                                        router.addRoute('chunk',res)
                                        sidebar.push(markRaw({
                                            title:item.title,
                                            path:item.path,
                                            icon:item.icon,
                                        }))
                                    }
                                })
                            })
                            next()
                        }

                    }else {
                        ElMessage.warning("登录失效，请重新登录")
                        next('/login')
                    }
                })
            }else {
                ElMessage.warning("登录失效，请重新登录")
                next('/login')
            }
        })
    }
})

export default router
